package com.huobao.yunying.activitys_hehuoren;

import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.activitys_log_reg.Act_Login;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.SessionUtils;
import com.huobao.yunying.utils.Utils;
import com.huobao.zhangying.R;

import org.simple.eventbus.EventBus;
import org.xutils.view.annotation.ViewInject;

import java.util.Map;

/**
 * 合伙人页面
 * 
 * @author Administrator
 *
 */
public class Act_Hehuoren extends HuobaoBaseActivity implements OnClickListener {
	@ViewInject(R.id.llyt)
	private View viewShouru;//收入
	@ViewInject(R.id.llyt_2)
	private View viewLiuyan;// 会员
	@ViewInject(R.id.llyt_4)
	private View viewHuiyuan;// 会员
	@ViewInject(R.id.llyt_3)
	private View viewGonghuoshang;// 供货商
	@ViewInject(R.id.llyt_5)
	private View viewDingdan;// 订单性情
	// 负责地区
	@ViewInject(R.id.tv_address)
	private TextView tv_address;
	// 今日订单
	@ViewInject(R.id.tv_1)
	private TextView tv_today_order;
	// 本月订单
	@ViewInject(R.id.tv_2)
	private TextView tv_yue_order;
	// 本月收入
	@ViewInject(R.id.tv_3)
	private TextView tv_shouru;
	// 账户余额
	@ViewInject(R.id.tv_4)
	private TextView tv_yue;
	//合伙人帐号
	@ViewInject(R.id.tv_hehuoren_zhanhao)
	private TextView tv_hehuoren_zhanhao;
	@ViewInject(R.id.tv_4_content)
	private TextView tv_4_content;
	
	
	
	
	
	@ViewInject(R.id.view_clear_cacle)
    private View view_clear_cacle;
    @ViewInject(R.id.tv_huancun)
    private TextView tv_huancun;
    @ViewInject(R.id.view_update)
    private View view_update;
    @ViewInject(R.id.tv_banben)
    private TextView tv_banben;
    @ViewInject(R.id.view_pingfen)
    private View view_pingfen;

	@Override
	protected int getContentView() {
		return R.layout.layout_hehuoren;
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void init() {
		mAbTitleBar.setVisibility(View.GONE);
		viewHuiyuan.setOnClickListener(this);
		viewGonghuoshang.setOnClickListener(this);
		viewLiuyan.setOnClickListener(this);
		viewShouru.setOnClickListener(this);
		viewDingdan.setOnClickListener(this);
		tv_4_content.setOnClickListener(this);
		
		
		view_clear_cacle.setOnClickListener(this);
		view_update.setOnClickListener(this);
		view_pingfen.setOnClickListener(this);
		tv_huancun.setText(ImageUtils.getCacelSize());
		tv_banben.setText(Utils.getAppVersionName(context));
		getData();
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.llyt_4:// 会员
			intent.setClass(context, Act_Huiyuan_List.class);
			break;
		case R.id.llyt_3:// 供货商
			intent.setClass(context, Act_Gonghuoshang_List.class);
			break;
		case R.id.llyt_2://留言
			intent.setClass(context, Act_Dailipin_Liuyan_List.class);
			break;
		case R.id.llyt://收入
			intent.setClass(context, Act_Shouru_List.class);
			break;
		case R.id.llyt_5:
			intent.setClass(context, Act_DingdanList.class);
			break;
		case R.id.tv_4_content:
			intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			intent.setClass(context, Act_Tixian.class);
			break;
			
		case R.id.view_clear_cacle:
			ImageUtils.clearCache();
			Toast.makeText(context, "缓存清除成功！", Toast.LENGTH_SHORT).show();
			tv_huancun.setText(ImageUtils.getCacelSize());
			return;
		case R.id.view_update:
			checkUpdate();
			return;
		case R.id.view_pingfen:
			Uri uri = Uri.parse("market://details?id="+getPackageName());    
			intent = new Intent(Intent.ACTION_VIEW,uri);    
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
			break;
		}
		startActivity(intent);
	}
	private void checkUpdate(){
		Utils.checkUpdate(this,true);
	}
	@Override
	protected boolean getCheckShowMsg() {
		return true;
	}
	private void getData() {
		Map<String, String> map = HttpUtil.getParams("app", "partner");
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				Hehuoren hehuoren = JSON.parseObject(jsonObject.toString(),Hehuoren.class);
				HehuorenUtil.saveHehuoren(context, hehuoren);
				tv_address.setText(jsonObject.getString("regionnames"));
				tv_today_order.setText(jsonObject.getString("tody_order") + "单");
				tv_yue_order.setText(jsonObject.getString("month_order") + "单");
				tv_shouru.setText("￥"+jsonObject.getString("month_incom"));
				tv_yue.setText("￥"+jsonObject.getString("account_balance"));
				tv_hehuoren_zhanhao.setText(jsonObject.getString("user_name"));
			}

			@Override
			public void onHttpError(String error) {
				EventBus.getDefault().post("", "GetUserMessageFail");
				finish();
			}
		});
	}
	
	public void onLogoutClick(View view){
		SessionUtils.loginout(context);
		EventBus.getDefault().post("", "goFristPage");
		Intent intent = new Intent(this, Act_Login.class);
		startActivity(intent);
		finish();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			EventBus.getDefault().post("", "goFristPage");
			finish();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
